package com.ymatou.iapi.settlement.testcase;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.settlement.parameter.AdRateProductInfo;
import com.ymatou.iapi.settlement.parameter.GenerateAnliSettlementReq;
import com.ymatou.iapi.settlement.service.GenerateAnliSettlementCall;
import com.ymatou.iapi.settlement.service.SettlementService;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.AnlihuiCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAnliOrderCallService;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.model.YmtAnliSettlementDetail;
import com.ymttest.database.model.YmtSettlementInvoice;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.YmtAnliSettlementDetailWrapper;
import com.ymttest.database.sqlwapper.YmtSettlementInvoiceWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

public class Ts_GenerateAnliSettlement {

	private static GenerateAnliSettlementReq generateAnliSettlementReq;
	private static GenerateAnliSettlementCall generateAnliSettlementCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	private static YmtAnliSettlementDetailWrapper ymtAnliSettlementDetailWrapper = new YmtAnliSettlementDetailWrapper();
	
	private static BigDecimal rebateMaxAmountPerProd = new BigDecimal("20.50");

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("安利会订单确认收货时生成结算单接口");
	}

	@Before
	public void caseUp() {
		generateAnliSettlementReq = new GenerateAnliSettlementReq();
		generateAnliSettlementCall = new GenerateAnliSettlementCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(generateAnliSettlementCall.getOpurl());
		
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(long sellerId, long orderId) throws InterruptedException {
		
		List<YmtAnliSettlementDetail> ymtAnliSettlementDetailList = ymtAnliSettlementDetailWrapper
				.selectByOrderId(orderId);
		
		List<YmtAnliSettlementDetail> expectAnliSettlementDetailList = new ArrayList<>();
		
		List<OrderDetailPo> orderDetailPoList = orderShardingWapper.selectOrderDetailbyOrderId(orderId);
		
		List<OrderDetailPo> sellerRebateRatioList = orderDetailPoList
				.stream()
				.filter(orderDetailPo -> Utils.zeroIfNull(orderDetailPo
						.getSellerRebateRatio()).compareTo(BigDecimal.ZERO) > 0)
				.collect(Collectors.toList());
		
		Logger.verifyEquals(sellerRebateRatioList.size(), ymtAnliSettlementDetailList.size(), "验证买手返点数据size");
		
		for (OrderDetailPo orderDetailPo : sellerRebateRatioList){
			
			String catalogId = orderDetailPo.getCatalogId();
			int productCount = orderDetailPo.getProductCount();
			
			Logger.comment("验证catalogId: " + catalogId);
			
			YmtAnliSettlementDetail actualAnliSettlementDetail = ymtAnliSettlementDetailWrapper
					.selectByOrderIdAndCatalogId(orderId, catalogId);
			
			YmtAnliSettlementDetail expectAnliSettlementDetail = new YmtAnliSettlementDetail();
			
			// 计算商品买手应收金额，不包含运费
			BigDecimal realPayAmount = SettlementService.getOrderDetailPaidAmountExcludeFreight(orderDetailPo);
			
			//单个商品实际支付金额
			BigDecimal realPayAmountPerProd = realPayAmount.divide(
					new BigDecimal(productCount), 32,
					RoundingMode.HALF_UP);
			
			//单个商品返点金额
			BigDecimal rebateAmountPerProd = rebateMaxAmountPerProd
					.compareTo(realPayAmountPerProd.multiply(
							orderDetailPo.getSellerRebateRatio())) < 0 ? rebateMaxAmountPerProd
					: realPayAmountPerProd.multiply(
							orderDetailPo.getSellerRebateRatio());
			
			expectAnliSettlementDetail.setOrderId(orderId);
			expectAnliSettlementDetail.setSellerId(sellerId);
			expectAnliSettlementDetail.setRebateRate(orderDetailPo
					.getSellerRebateRatio().setScale(2, RoundingMode.HALF_UP));
			expectAnliSettlementDetail
					.setRebateAmountPerProd(rebateAmountPerProd);
			expectAnliSettlementDetail
					.setRebateMaxAmountPerProd(new BigDecimal("20.50"));
			expectAnliSettlementDetail.setProductNum(productCount);
			expectAnliSettlementDetail.setRebateAmount(rebateAmountPerProd
					.multiply(new BigDecimal(productCount)).setScale(2, RoundingMode.HALF_UP));
			expectAnliSettlementDetail.setRealPayAmount(realPayAmount);
			expectAnliSettlementDetail.setAnliPromotionRuleId(orderDetailPo
					.getAnliPromotionRuleId());
			expectAnliSettlementDetail
					.setRealPayAmountPerProd(realPayAmountPerProd.setScale(2, RoundingMode.HALF_UP));
			
			Logger.verifyEquals(expectAnliSettlementDetail.getOrderId(),
					actualAnliSettlementDetail.getOrderId(), "验证orderId");
			Logger.verifyEquals(expectAnliSettlementDetail.getSellerId(),
					actualAnliSettlementDetail.getSellerId(), "验证sellerId");
			Logger.verifyEquals(expectAnliSettlementDetail.getProductNum(),
					actualAnliSettlementDetail.getProductNum(), "验证productNum");
			Logger.verifyEquals(expectAnliSettlementDetail.getAnliPromotionRuleId(),
					actualAnliSettlementDetail.getAnliPromotionRuleId(), "验证anliPromotionRuleId");
			DataUtil.verifyDecimalEquals(
					expectAnliSettlementDetail.getRebateRate(),
					actualAnliSettlementDetail.getRebateRate(), "验证rebateRate");
			DataUtil.verifyDecimalEquals(
					expectAnliSettlementDetail.getRebateAmountPerProd(),
					actualAnliSettlementDetail.getRebateAmountPerProd(), "验证rebateAmountPerProd");
			DataUtil.verifyDecimalEquals(
					expectAnliSettlementDetail.getRebateMaxAmountPerProd(),
					actualAnliSettlementDetail.getRebateMaxAmountPerProd(), "验证rebateMaxAmountPerProd");
			DataUtil.verifyDecimalEquals(
					expectAnliSettlementDetail.getRebateAmount(),
					actualAnliSettlementDetail.getRebateAmount(), "验证rebateAmount");
			DataUtil.verifyDecimalEquals(
					expectAnliSettlementDetail.getRealPayAmount(),
					actualAnliSettlementDetail.getRealPayAmount(), "验证realPayAmount");
			DataUtil.verifyDecimalEquals(
					expectAnliSettlementDetail.getRealPayAmountPerProd(),
					actualAnliSettlementDetail.getRealPayAmountPerProd(), "验证realPayAmountPerProd");
			
			if (AnlihuiCallService.isExistSettlement(orderId)) {
				
				YmtSettlementInvoiceWapper wrapper = new YmtSettlementInvoiceWapper();
				
				BigDecimal costAmount = wrapper
						.selectYmtAdRateInfoByOrderIdAndCatalogId(orderId,
								orderDetailPo.getCatalogId()).getCostAmount();
				
				if (actualAnliSettlementDetail.getRebateAmount().compareTo(Utils.zeroIfNull(costAmount)) < 0) {
					
					expectAnliSettlementDetail.setNoNeedSettle(true);
					expectAnliSettlementDetail.setRebateAmount(BigDecimal.ZERO);
				} else
					expectAnliSettlementDetail.setNoNeedSettle(false);
			}
			
			Logger.verifyEquals(
					expectAnliSettlementDetail.getNoNeedSettle(),
					actualAnliSettlementDetail.getNoNeedSettle(), "验证noNeedSettle");
			
			expectAnliSettlementDetailList.add(expectAnliSettlementDetail);
		}
		
		Map<String, Object> map = new HashMap<>();

		map.put("orderId", orderId);
		map.put("businessType", 17);

		List<YmtSettlementInvoice> ymtSettlementInvoiceList = new YmtSettlementInvoiceWapper()
				.selectYmtSettlementInvoice(map);

		BigDecimal expectSettlementAmount = expectAnliSettlementDetailList.stream()
				.map(ad -> ad.getRebateAmount())
				.reduce(BigDecimal.ZERO, BigDecimal::add);

		DataUtil.verifyDecimalEquals(expectSettlementAmount,
				ymtSettlementInvoiceList.get(0).getCashAmount(),
				"校验实际结算的安利费用");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_GenerateAnliSettlement_001() {
		Logger.start(true, "正常购买一份普通商品,已结算，验证结算数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long memberId = 2002;
			int memberOrderType = 0;

			TradingServiceResp placeOrder = TradingPlaceAnliOrderCallService
					.placeAnlihuiOrder(BigDecimal.ZERO, false, memberId,
							memberOrderType);
			Long userId = placeOrder.getUserId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue());
			TradingSyncService.getSyncResult(orderId);
			
			Thread.sleep(10000);

			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			//生成安利结算
			generateAnliSettlementReq.setOrderId(orderId);
			
			generateAnliSettlementCall.setData(generateAnliSettlementReq);
			generateAnliSettlementCall.callService();
			
			Thread.sleep(10000);

			verifyDb(sellerId, orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_GenerateAnliSettlement_002() {
		Logger.start(true, "正常购买一份普通商品,两个商品均设置了返点比率，已结算，验证结算数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long memberId = 2002;
			int memberOrderType = 0;

			TradingServiceResp placeOrder = TradingPlaceAnliOrderCallService
					.placeMultiProAnlihuiOrder(BigDecimal.TEN, false, memberId,
							memberOrderType);
			Long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue());
			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(10000);

			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			//生成安利结算
			generateAnliSettlementReq.setOrderId(orderId);
			
			generateAnliSettlementCall.setData(generateAnliSettlementReq);
			generateAnliSettlementCall.callService();
			
			Thread.sleep(10000);
			
			verifyDb(sellerId, orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Ts_GenerateAnliSettlement_003() {
		Logger.start(true, "正常购买一份普通商品,两个商品，其中一个设置了返点比率，一个未设置，已结算，验证结算数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long memberId = 2002;
			int memberOrderType = 0;
			
			TradingServiceResp placeOrder = TradingPlaceAnliOrderCallService
					.placeMultiProAnlihuiOrder(BigDecimal.TEN, false, memberId,
							memberOrderType, false);
			Long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue());
			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(10000);
			
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);
			
			//确认收货
			TradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			//生成安利结算
			generateAnliSettlementReq.setOrderId(orderId);
			
			generateAnliSettlementCall.setData(generateAnliSettlementReq);
			generateAnliSettlementCall.callService();
			
			Thread.sleep(10000);

			verifyDb(sellerId, orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Ts_GenerateAnliSettlement_004() {
		Logger.start(true, "正常购买一份普通商品,确认收货前，商品依次全部退款");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long memberId = 2002;
			int memberOrderType = 0;
			TradingServiceResp placeOrder = TradingPlaceAnliOrderCallService
					.placeAnlihuiOrder(BigDecimal.TEN, false, memberId, memberOrderType);
			Long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue());
			TradingSyncService.getSyncResult(orderId);
			
			//生成安利结算
			generateAnliSettlementReq.setOrderId(orderId);
			
			generateAnliSettlementCall.setData(generateAnliSettlementReq);
			generateAnliSettlementCall.callService();
			
			Thread.sleep(10000);

			AnlihuiCallService.anliOrderRefund(placeOrder.getOrderList()
					.get(0), true, 0);

			Thread.sleep(4000);

			// 支付网关退款回调
			PayGateWayCallService.refundNotifyCall("13",
					thirdTradingId.toString(), userId.intValue());

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			TradingSyncService.getSyncResult(orderId);
			
			verifyDb(sellerId, orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Ts_GenerateAnliSettlement_005() {
		Logger.start(true, "正常购买一份普通商品,确认收货前，商品部分退货退款");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long memberId = 2002;
			int memberOrderType = 0;
			TradingServiceResp placeOrder = TradingPlaceAnliOrderCallService
					.placeAnlihuiOrder(BigDecimal.TEN, false, memberId, memberOrderType);
			Long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue());
			TradingSyncService.getSyncResult(orderId);
			
			//生成安利结算
			generateAnliSettlementReq.setOrderId(orderId);
			
			generateAnliSettlementCall.setData(generateAnliSettlementReq);
			generateAnliSettlementCall.callService();
			
			Thread.sleep(10000);

			AnlihuiCallService.anliOrderRefund(placeOrder.getOrderList()
					.get(0), true, 1);

			Thread.sleep(4000);

			// 支付网关退款回调
			PayGateWayCallService.refundNotifyCall("13",
					thirdTradingId.toString(), userId.intValue());

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			TradingSyncService.getSyncResult(orderId);
			
			verifyDb(sellerId, orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_GenerateAnliSettlement_006() {
		Logger.start(true, "新交易安利会返点订单,买手返点金额大于广告费，校验广告费率信息生成成功");
		try {
			
			BigDecimal sellerRebateRatio = new BigDecimal(
					new Random().nextFloat() / 10).setScale(4, RoundingMode.DOWN);

			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeAnlihuiOrder(BigDecimal.ZERO, false, 0,
							Arrays.asList(sellerRebateRatio, sellerRebateRatio.add(new BigDecimal("0.001"))));

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(placeOrderResp.getUserId(), payAmount,
					placeOrderResp.getTradingId());
			
			Thread.sleep(5000);
			
			//生成安利结算
			generateAnliSettlementReq.setOrderId(orderId);
			
			generateAnliSettlementCall.setData(generateAnliSettlementReq);
			generateAnliSettlementCall.callService();

			List<OrderDetailPo> orderInfoList = placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(sellerRebateRatio.subtract(new BigDecimal("0.002")));

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);
			}
			
			SettlementService.generateAdRateInfo(orderId, adRateProductInfoList);
			
			Thread.sleep(10000);

			verifyDb(sellerId, orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_GenerateAnliSettlement_007() {
		Logger.start(true, "新交易安利会返点订单,买手返点金额小于广告费，校验广告费率信息生成成功");
		try {
			
			BigDecimal sellerRebateRatio = new BigDecimal(
					new Random().nextFloat() / 10).setScale(4, RoundingMode.DOWN);

			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeAnlihuiOrder(BigDecimal.ZERO, false, 0,
							Arrays.asList(sellerRebateRatio, sellerRebateRatio.add(new BigDecimal("0.001"))));

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(placeOrderResp.getUserId(), payAmount,
					placeOrderResp.getTradingId());
			
			Thread.sleep(5000);
			
			//生成安利结算
			generateAnliSettlementReq.setOrderId(orderId);
			
			generateAnliSettlementCall.setData(generateAnliSettlementReq);
			generateAnliSettlementCall.callService();

			List<OrderDetailPo> orderInfoList = placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(sellerRebateRatio.add(new BigDecimal("0.002")));

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);
			}
			
			SettlementService.generateAdRateInfo(orderId, adRateProductInfoList);

			Thread.sleep(10000);
			
			verifyDb(sellerId, orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_GenerateAnliSettlement_008() {
		Logger.start(true, "新交易安利会返点订单,买手返点金额等于广告费，校验广告费率信息生成成功");
		try {
			
			BigDecimal sellerRebateRatio = new BigDecimal(
					new Random().nextFloat() / 10).setScale(4, RoundingMode.DOWN);

			TradingServiceResp placeOrderResp = SettlementService
					.placeAnlihuiOrderExcludeFreight(Arrays.asList(
							sellerRebateRatio,
							sellerRebateRatio.add(new BigDecimal("0.001"))));

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(placeOrderResp.getUserId(), payAmount,
					placeOrderResp.getTradingId());
			
			Thread.sleep(10000);
			
			//生成安利结算
			generateAnliSettlementReq.setOrderId(orderId);
			
			generateAnliSettlementCall.setData(generateAnliSettlementReq);
			generateAnliSettlementCall.callService();

			List<OrderDetailPo> orderInfoList = placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {	

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(sellerRebateRatio);

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);
			}

			SettlementService.generateAdRateInfo(orderId, adRateProductInfoList);

			Thread.sleep(10000);
			
			verifyDb(sellerId, orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		
		Ts_GenerateAnliSettlement test = new Ts_GenerateAnliSettlement();
		test.verifyDb(24635724L, 7663408699L);
	}
}
